<?php
/**
 * Created by PhpStorm.
 * User: ZCY
 * Date: 2018/10/18
 * Time: 17:30
 */


namespace api\controllers;
use Yii;
use api\components\ApiController;
use yii\base\Exception;
use yii\web\NotFoundHttpException;


class RedisController extends ApiController
{

    /** hash */
    public function actionHash()
    {
        //批量设置hash的键值对
        /*Yii::$app->redis->hmset('application_users','app_id_1',1000);
        Yii::$app->redis->hmset('application_users','app_id_2',1001);*/
        //设置hash单个的键值
       // Yii::$app->redis->hset('application_users','app_id_3',1001);
        //获取hash单个的键值
        //echo Yii::$app->redis->hget('application_users','app_id_2');
        //var_dump(Yii::$app->redis->hget('application_users','app_id_4'));  //不存在的键值会返回null
        //var_dump(Yii::$app->redis->hmget('application_users','app_id_1','app_id_2'));  //批量获取键值对
        //Yii::$app->redis->hincrby('application_users','app_id_1',5);     //hash 指定建自增
        //var_dump(Yii::$app->redis->hget('application_user','app_id_2'));
        var_dump(Yii::$app->redis->hexists('application_users','app_id_5'));
    }

    /**
     * zset
     */
    public function actionZSet()
    {
        $redis = Yii::$app->redis;
  /*      $redis->zadd('application_hot_search',5,'浏览器');
        $redis->zadd('application_hot_search',2,'邮箱');
        $redis->zadd('application_hot_search',3,'游戏');
        $redis->zadd('application_hot_search',4,'运动');*/
        //var_dump($redis->zrange('application_hot_search',0,-1,'WITHSCORES'));
        var_dump($redis->zrange('application_hot_search',0,-1));
        var_dump($redis->zscore('application_hot_search','邮箱'));
    }

    /**
     * list
     */
    public function actionList()
    {
        $redis = Yii::$app->redis;
        /*$redis->lpush('list_test',"aaa");
        $redis->lpush('list_test',"bbb");
        $redis->lpush('list_test',"ccc");
        $redis->lpush('list_test',"ddd");*/
        $data = $redis->lrange('list_test',0,-1);
        var_dump($data);die;
    }

    public function actionSet()
    {
        $redis = Yii::$app->redis;
        $data = [
            [
                '13199003445',
                'BTY',
                1000
            ],
            [
                '13166091930',
                'BTY',
                1000
            ],
        ];
        $data_json = json_encode($data);
        echo $data_json;
        $redis->set('release_member_data',$data_json,'EX',100);           //100s过期时间
    }



    public function  actionTestError()
    {
        echo "error test";
        throw new NotFoundHttpException();
    }




}
